#ifndef _KNN_H
#define _KNN_H

#include "Classifier.h"

class KNN : public Classifier {
public:
    virtual void train(vector<Sample *> &sampleList);
    
    virtual int getK() {
        return m_K;
    }

    /** K is best to be under 8, for there is a bubble sort in the classify method */
    virtual void preClassify(unsigned short K) {
        if(K > m_sampleList->size()) {
            m_K = m_sampleList->size();
        } else {
            m_K = K;
        }
    }

    virtual vector<Sample *> * getSampleList() {
        return m_sampleList;
    }

    /**
     *@param copyList if need a exactly copy of trained sampleList, or load from file, it is true, otherwise false
     */
    KNN(SampleSpace &sampleSpace, bool copyList): Classifier(sampleSpace) {
        m_K = 1;
        m_copyList = copyList;
        m_sampleList = NULL;
    }

    virtual ~KNN() {
        releaseSampleList();
    }

protected:
    int m_K;

    bool m_copyList;

    vector<Sample *> *m_sampleList;

    virtual void releaseSampleList() {
        if (m_sampleList != NULL) {
            if(m_copyList) {
                m_sampleSpace->deleteSampleArray(*m_sampleList);
                delete m_sampleList;
            }
            
            m_sampleList = NULL;
        }
    }

};

#endif
